<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activIdsAdmin']))
{
	header("Location:checkLogin.php");
}
else
{

if(isset($_REQUEST['submit']))
{
	
  $userId       = isset($_POST['userId']) ? $_POST['userId'] : 0;
  $activeUserId = ($_SESSION['s_activIdsAdmin']);
  $contractId   = $_POST['contractId'];
  $uploadDateTime = date('Y-m-d h:i:s');

 	if((isset($_POST['contractId']) && ($_POST['contractId']) == 0) || (isset($_POST['contractNo']) && ($_POST['contractNo']) !=''))
  {
    $contDate     = $_POST['contDateYear']."-".$_POST['contDateMonth']."-".$_POST['contDateDay'];
    $advRptDate   = $_POST['advDateYear']."-".$_POST['advDateMonth']."-".$_POST['advDateDay'];
    $contractNo   = (isset($_POST['contractNo']) && $_POST['contractNo'] != '') ? $_POST['contractNo'] : "";
    $contQty      = (isset($_POST['contQty']) && $_POST['contQty'] > 0) ? $_POST['contQty'] : 0;
    $conPrice     = (isset($_POST['conPrice']) && $_POST['conPrice'] > 0) ? $_POST['conPrice'] : 0;
    $conValue     = (isset($_POST['conValue']) && $_POST['conValue'] > 0) ? $_POST['conValue'] : 0;
    $numOfInvoice = (isset($_POST['numOfInvoice']) && $_POST['numOfInvoice'] > 0) ? $_POST['numOfInvoice'] : 0;
    $advAmount    = (isset($_POST['advAmount']) && $_POST['advAmount'] > 0) ? $_POST['advAmount'] : 0;
    
    $contractDetail = "INSERT INTO contract (userId,contractNo,contDate,contQty,conPrice,conValue,numOfInvoice,advAmount,advRptDate)
                             VALUES(".$userId.",'".$contractNo."','".$contDate."',".$contQty.",".$conPrice.",".$conValue.",".$numOfInvoice.",".$advAmount.",
                             '".$advRptDate."')";
    $contractDetailRes = mysql_query($contractDetail);
    $contractId = mysql_insert_id();
    if(!$contractDetailRes)
    {
    	echo "Contract Insert Fail";
    }
    else
    {
      header("Location:userDoc.php");
    }
  }
  
  if(isset($_POST['invoiceNo']) && ($_POST['invoiceNo']) != '')
  {
    $dateOfLoading  = $_POST['loadDateYear']."-".$_POST['loadDateMonth']."-".$_POST['loadDateDay'];
    $etdDate        = $_POST['etdDateYear']."-".$_POST['etdDateMonth']."-".$_POST['etdDateDay'];
    $invoiceNo      = (isset($_POST['invoiceNo']) && $_POST['invoiceNo'] != '') ? $_POST['invoiceNo'] : "";
    $qty            = (isset($_POST['qty']) && $_POST['qty'] != "") ? $_POST['qty'] : "";
    $price          = (isset($_POST['price']) && $_POST['price'] > 0) ? $_POST['price'] : 0;
    $value          = (isset($_POST['value']) && $_POST['value'] > 0) ? $_POST['value'] : 0;
    $vessel         = (isset($_POST['vessel']) && $_POST['vessel'] != "") ? $_POST['vessel'] : "";
    $etd            = (isset($_POST['etd']) && $_POST['etd'] > 0) ? $_POST['etd'] : 0;
    
    $invoiceDetail = "INSERT INTO  invoice (userId,contractId,invoiceNo,qty,price,value,dateOfLoading,vessel,etdDate)
                             VALUES(".$userId.",".$contractId.",'".$invoiceNo."','".$qty."','".$price."','".$value."','".$dateOfLoading."','".$vessel."',
                             '".$etdDate."')";
    $invoiceDetailRes = mysql_query($invoiceDetail);
    $lastinvoiceId = mysql_insert_id();
    if(!$invoiceDetailRes)
    {
    	echo "Invoice Insert Fail";
    }
    else
    {
    	$insertItem  = "INSERT INTO userdoc (userId,uploadedBy,contractId,invoiceId,uploadDateTime)
		                  VALUES(".$userId.",".$activeUserId.",'".$contractId."',".$lastinvoiceId.",'".$uploadDateTime."')";
		  $insertItemRes = mysql_query($insertItem);
      header("Location:userDoc.php");
    }
  }
}
}
	
$o=0;
$contractArray = array();
$selectContract = "SELECT contractId,contractNo
                     FROM contract";
$selectContractRes = mysql_query($selectContract);
while($contractRow = mysql_fetch_array($selectContractRes))
{
	$contractArray['contractId'][$o] = $contractRow['contractId'];
	$contractArray['contractNo'][$o] = $contractRow['contractNo'];
	$o++;
}

$l=0;
$invoiceArray = array();
$selectInvoice = "SELECT invoiceId,invoiceNo
                    FROM invoice";
$selectInvoiceRes = mysql_query($selectInvoice);
while($invoiceRow = mysql_fetch_array($selectInvoiceRes))
{
	$invoiceArray['invoiceId'][$l] = $invoiceRow['invoiceId'];
	$invoiceArray['invoiceNo'][$l] = $invoiceRow['invoiceNo'];
	$l++;
}

$i=0;
$userArray = array();
$selectUsers = "SELECT userId,company
                  FROM user";
$selectUsersRes = mysql_query($selectUsers);
while($userRow = mysql_fetch_array($selectUsersRes))
{
	$userArray['userId'][$i]   = $userRow['userId'];
	$userArray['company'][$i]  = $userRow['company'];
	$i++;
}

$m=0;
$contractDetailArray = array();
$selectContract = "SELECT contractId,contractNo,DATE_FORMAT(contDate,'%d-%m-%Y') AS contDate,contQty,conPrice,conValue,numOfInvoice,advAmount,
                          DATE_FORMAT(advRptDate,'%d-%m-%Y') AS advRptDate,company
                     FROM contract
                   LEFT JOIN user ON user.userId = contract.userId";
$selectContractRes= mysql_query($selectContract);
while($contractRow = mysql_fetch_array($selectContractRes))
{
	$contractDetailArray[$m]['contractId']   = $contractRow['contractId'];
	$contractDetailArray[$m]['contractNo']   = $contractRow['contractNo'];
	$contractDetailArray[$m]['contDate']     = $contractRow['contDate'];
	$contractDetailArray[$m]['contQty']      = $contractRow['contQty'];
	$contractDetailArray[$m]['conPrice']     = $contractRow['conPrice'];
	$contractDetailArray[$m]['conValue']     = $contractRow['conValue'];
	$contractDetailArray[$m]['numOfInvoice'] = $contractRow['numOfInvoice'];
	$contractDetailArray[$m]['advAmount']    = $contractRow['advAmount'];
	$contractDetailArray[$m]['advRptDate']   = $contractRow['advRptDate'];
	$contractDetailArray[$m]['company']      = $contractRow['company'];
	$m++;
}
	
include("bottom.php");
$smarty->assign("userArray",$userArray);
$smarty->assign("invoiceArray",$invoiceArray);
$smarty->assign("contractArray",$contractArray);
$smarty->assign("contractDetailArray",$contractDetailArray);
$smarty->display("userDoc.tpl");
?>